# Create your views here
# -*- coding: utf-8 -*-
from django.shortcuts import render_to_response
from django.http import HttpResponse, HttpResponseRedirect
from myblog.comment.forms import comment_form 
from myblog.comment.models import Comment, Photo_Comment, About_Comment
from post.models import Post

def create_comment(request,pid):
    if request.method == "POST":
        form = comment_form(request.POST)
        post = Post.objects.get(id=pid)
        if form.is_valid():
            cd = form.cleaned_data
            comm = Comment.objects.create(name = cd['name'], content = cd['content'], post = post)
            comm.save
            return render_to_response('comment/comment_ajax.html', locals())
        else:
            info = '非法操作'
            return render_to_response('errors.html', locals())
        
def create_photo_comment(request):
    if request.method == "POST":
        form = comment_form(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            comm = Photo_Comment.objects.create(name = cd['name'], content = cd['content'])
            comm.save
            return render_to_response('comment/comment_ajax.html', locals())
        else:
            info = '非法操作'
            return render_to_response('errors.html', locals())
        
def create_about_comment(request):
    if request.method == "POST":
        form = comment_form(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            comm = About_Comment.objects.create(name = cd['name'], content = cd['content'])
            comm.save
            return render_to_response('comment/comment_ajax.html', locals())
        else:
            info = '非法操作'
            return render_to_response('errors.html', locals())
        
def delete_comment(request, id):
    user = request.user
    if user.is_superuser:
        comment = Comment.objects.get(id=id)
        comment.delete()
        return HttpResponseRedirect('/')
    else:
        return HttpResponse('权限不够')
    
def delete_photo_comment(request, id):
    user = request.user
    if user.is_superuser:
        comment = Photo_Comment.objects.get(id=id)
        comment.delete()
        return HttpResponseRedirect('/')
    else:
        return HttpResponse('权限不够')

def delete_about_comment(request, id):
    user = request.user
    if user.is_superuser:
        comment = About_Comment.objects.get(id=id)
        comment.delete()
        return HttpResponseRedirect('/')
    else:
        return HttpResponse('权限不够')